package com.hzhjhh.view.manageView.menus.storage.inOutStorage;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.entity.Stock;
import com.hzhjhh.entity.StockMovementRecord;
import com.hzhjhh.mapper.StockMapper;
import com.hzhjhh.mapper.StockMovementRecordMapper;
import com.hzhjhh.means.Select_Means;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.storage.InOutTableDialog;

/**
 * 菜单栏初始化-仓库-进库/出库-提交按钮
 * 
 * @author 12083
 *
 */
public class SubmitBt {

	private static InOutTableDialog inOutTableDialog;
	private static StockMapper stockMapper;
	private static StockMovementRecordMapper stockMovementRecordMapper;
	/**
	 * 按钮
	 */
	public static JButton btInit(InOutTableDialog inOutTableDialog) {
		
		SubmitBt.inOutTableDialog = inOutTableDialog;
		JComboBox jComboBox1 = inOutTableDialog.getJComboBox1();
		JButton reset = new JButton();
		reset.setBounds(477, 409, 111, 30);
        reset.setText("提交");
        reset.addActionListener(event -> {
        	JTable jTable1 = inOutTableDialog.getJTable1();
        	Vector vector1 = inOutTableDialog.getVector1();
        	JScrollPane jScrollPane1 = inOutTableDialog.getJScrollPane1();
        	
        	if (jComboBox1.getSelectedItem().toString().equals("入库")) {
    			record2DB("1");
    		}
    		if (jComboBox1.getSelectedItem().toString().equals("出库")) {
    			record2DB("0");
    		}
        });
        
        // 初始化数据库操作mapper
        SqlSession sqlSession = DBUtils.getSqlSession(true);
        stockMapper = sqlSession.getMapper(StockMapper.class);
        stockMovementRecordMapper = sqlSession.getMapper(StockMovementRecordMapper.class);
        
		return reset;
	}
	public static void record2DB(String inout) {
		JTextField tfMerchandiseCode = inOutTableDialog.getTfMerchandiseCode();
		Vector vector1 = inOutTableDialog.getVector1();
		
		Vector select1 = new Vector();
		select1 = Select_Means.Select_sort2(tfMerchandiseCode.getText());
		if (select1.size() == 0) {
			JOptionPane.showMessageDialog(inOutTableDialog, "商品不存在", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		// 处理数据
		Vector insertData = new Vector();

		List<StockMovementRecord> addList = new ArrayList<>();
		List<Stock> updateList = new ArrayList<>();
		int i = 0;
		Date now = new Date();
		while (i < vector1.size()) {
			insertData = (Vector) vector1.get(i);
			Integer num1 = new Integer(insertData.get(1).toString());
			Double num2 = new Double(insertData.get(2).toString());
			String merchandiseCode = insertData.get(0).toString();
			Stock stock = new Stock();
			stock.setMerchandiseCode(merchandiseCode);
			// 入库
			if (inout.equals("1")) {
				stock.setNumber(num1);
				updateList.add(stock);
			}
			// 出库
			if (inout.equals("0")) {
				stock.setNumber(0 - num1);
				updateList.add(stock);
			}

			StockMovementRecord movementRecord = new StockMovementRecord();
			movementRecord.setMerchandiseCode(merchandiseCode);
			movementRecord.setNumber(num1);
			movementRecord.setPricePerOne(new BigDecimal(insertData.get(2).toString()));
			movementRecord.setTotalAmt(new BigDecimal(insertData.get(3).toString()));
			movementRecord.setOrderDate(now);
			movementRecord.setRepositoryId(insertData.get(5).toString());
			movementRecord.setCreatedBy(insertData.get(6).toString());
			movementRecord.setFiller(insertData.get(7).toString());
			movementRecord.setType(inout);
			
			addList.add(movementRecord);
			i ++;
//			int j = Insert_stock.Insert_EnterStock(insertData.get(0).toString(), num1, num2,
//					insertData.get(4).toString(), insertData.get(5).toString(), insertData.get(6).toString(),
//					insertData.get(7).toString(), inout);
//			if (j == 0) {
//				i++;
//				String str = "第" + i + "条记录数据写入失败";
//				JOptionPane.showMessageDialog(inOutTableDialog, str, "错误", JOptionPane.ERROR_MESSAGE);
//				return;
//			}
//			i++;
//			if (i == vector1.size()) {
//				JOptionPane.showMessageDialog(inOutTableDialog, "数据录入成功", "提示", JOptionPane.INFORMATION_MESSAGE);
//			}
		}
		// 批量操作数据库
		if (CollectionUtils.isNotEmpty(updateList)) {
			stockMapper.batchUpdateByNumberAndCode(updateList);			
		}
		if (CollectionUtils.isNotEmpty(addList)) {			
			stockMovementRecordMapper.batchInsert(addList);
		}
		JOptionPane.showMessageDialog(inOutTableDialog, "数据录入成功", "提示", JOptionPane.INFORMATION_MESSAGE);
	}
}
